﻿using IStrong.EC.Web.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using IStrong.EC.WebApi.Results.Entity.Daily;
using IStrong.EC.WebApi.Results.Repository;

namespace IStrong.EC.WebApi.Results.Controllers
{
    /// <summary>
    /// Api控制器模板
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class WeixinuserController : BaseController
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<WeixinuserController> _logger;

        /// <summary>
        /// 配置
        /// </summary>
        private readonly IConfiguration _config;

        private readonly WeixinusersRepository _weixinuserRepository;

        /// <summary>
        /// 注入服务 
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="logger"></param>
        /// <param name="config"></param>
        public WeixinuserController(ILogger<WeixinuserController> logger, IConfiguration config,
            WeixinusersRepository weixinuserRepository)
        {
            _logger = logger;
            _config = config;
            _weixinuserRepository = weixinuserRepository;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="User"></param>
        /// <returns></returns>
        [HttpPost("wxlogin")]
        public async Task<JsonResult> PostWxLogin([FromBody] Weixinusers User)
        {
            var Result = _weixinuserRepository.AddWeixinuser(User);
            return await Task.FromResult(Json(Result));
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        [HttpGet("userinfo")]
        public async Task<JsonResult> GetUserInfo(string OpenId)
        {
            try
            {

                var user = _weixinuserRepository.GetUserInfo(new { OpenId });
                return await Task.FromResult(Json(user));
            }
            catch (Exception ex)
            {
                return await Task.FromResult(Json(ex));
            }
        }


    }
}
